package com.hubiao.pay.merchant.common.intercept;

import com.hubiao.pay.common.domain.BusinessException;
import com.hubiao.pay.common.domain.CommonErrorCode;
import com.hubiao.pay.common.domain.ErrorCode;
import com.hubiao.pay.common.domain.RestErrorResponse;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * Description 全局异常处理类
 * ControllerAdvice注解与ExceptionHandler注解一起使用来实现全局异常处理
 *
 * @author hubiao
 * @since 2020-11-08 18:53
 */
@ControllerAdvice
public class GlobalExceptionHandler {


    /**
     * 捕获Exception异常
     *
     * @return
     * @ResponseBody 返回json数据
     * @ResponseStatus 定义异常状态统一为500 系统异常
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public RestErrorResponse processException(HttpServletRequest request,
                                              HttpServletResponse response,
                                              Exception e) {


        //解析异常信息
        //如果是系统自定义异常异常则取出他的errorCode和errorMessage
        if (e instanceof BusinessException) {
            //解析系统自定义异常
            BusinessException businessException = (BusinessException) e;
            ErrorCode errorCode = businessException.getErrorCode();
            //错误代码
            int code = errorCode.getCode();
            //错误信息
            String desc = errorCode.getDesc();
            return new RestErrorResponse( String.valueOf( code ), desc );
        } else if (e instanceof NullPointerException) {
            return new RestErrorResponse( String.valueOf( CommonErrorCode.E_400101.getCode() ), CommonErrorCode.E_400101.getDesc() );
        } else if (e instanceof ClassCastException) {
            return new RestErrorResponse( String.valueOf( CommonErrorCode.E_400102.getCode() ), CommonErrorCode.E_400102.getDesc() );
        } else if (e instanceof IllegalArgumentException) {
            return new RestErrorResponse( String.valueOf( CommonErrorCode.E_400103.getCode() ), CommonErrorCode.E_400103.getDesc() );
        } else if (e instanceof IndexOutOfBoundsException) {
            return new RestErrorResponse( String.valueOf( CommonErrorCode.E_400104.getCode() ), CommonErrorCode.E_400104.getDesc() );
        } else if (e instanceof IOException) {
            return new RestErrorResponse( String.valueOf( CommonErrorCode.E_400105.getCode() ), CommonErrorCode.E_400105.getDesc() );
        } else if (e instanceof NoSuchMethodException) {
            return new RestErrorResponse( String.valueOf( CommonErrorCode.E_400106.getCode() ), CommonErrorCode.E_400106.getDesc() );
        } else if (e instanceof SQLException) {
            return new RestErrorResponse( String.valueOf( CommonErrorCode.E_400107.getCode() ), CommonErrorCode.E_400107.getDesc() );
        }
        //则是未知异常 统一定义为99999系统错误异常
        return new RestErrorResponse( String.valueOf( CommonErrorCode.UNKNOWN.getCode() ), CommonErrorCode.UNKNOWN.getDesc() );
    }
}
